<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 15:05
 */

namespace App\Http\Controllers\Api\Doctor;

use App\Http\Controllers\ApiController;
use App\LogicService\Contracts\UserGroupLogicContract;
use App\LogicService\Contracts\UserGroupMemberLogicContract;
use App\Transformers\UserGroupMemberTransformer;
use Illuminate\Http\Request;
use RongCloud;

class UserGroupMemberController extends ApiController
{


    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    protected $userGroupMember;

    protected $userGroup;


    /**
     * UserGroupMemberController constructor.
     * @param Request $request
     * @param UserGroupMemberLogicContract $userGroupMember
     * @param UserGroupLogicContract $userGroup
     */

    public function __construct(Request $request, UserGroupMemberLogicContract $userGroupMember,
                                UserGroupLogicContract $userGroup)
    {
        parent::__construct();

        $this->request = $request;
        $this->rong = New RongCloud(config('app.rongcloud_appKey'), config('app.rongcloud_appSecret'));
        $this->userGroupMember = $userGroupMember;
        $this->userGroup = $userGroup;
    }


    /**
     * 获取群组用户列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGroupMemberList()
    {
        $this->validate($this->request, [
            'group_id' => 'required|integer',
        ]);

        $user = \Auth::user();

        $data = $this->userGroupMember->getGroupMemberList($this->request->input('group_id'));

        return $this->collection($data, new UserGroupMemberTransformer());
    }


    /**
     * 加入群组
     * @return \Illuminate\Http\JsonResponse
     */
    public function store()
    {
        $this->validate($this->request, [
            'group_id' => 'required|integer',
        ]);

        $user = \Auth::user();

        $group_id = $this->request->input('group_id');

        $group = $this->userGroup->show($group_id);

        $data = 0;

        if ($user->type == 1) {
            $rong_account = 'patient_' . $user->account;
        } else {
            $rong_account = 'doctor_' . $user->account;
        };

        if ($group) {
            $data = $this->userGroupMember->store($this->request->input('group_id'), $rong_account);

            $user_ids = [$rong_account];
            if ($data) {

                $this->rong->group()->join($user_ids, $group->id, $group->name);

                $content = array(
                    "operatorUserId"=>$rong_account,
                    "operation"=>"Add",
                    "data"=>array(
                        "operatorNickname"=>$user->name,
                        "targetUserIds"=>[$rong_account],
                        "targetUserDisplayNames"=>[$user->name]),
                    "message"=>$user->name."加入群组",
                    "extra"=>""
                );
                $this->rong->message()
                    ->publishGroup('doctor_'.$user->account, $group_id, 'RC:GrpNtf',json_encode($content), 'thisisapush', '{\"pushData\":\"修改本群名\"}', '1', '1', '0');

            } else {
                return $this->setMessage('加入群组失败')->setCode('400004')->response();
            }
        }

        return $this->response(['result' => $data]);
    }


    public function delete()
    {
        $this->validate($this->request, [
            'group_id' => 'required|integer',
        ]);

        $user = \Auth::user();
        $user_account = 'doctor_' . $user->account;
        if ($user->type == 1) {
            $user_account = 'patient_' . $user->account;
        }

        $group_id = $this->request->input('group_id');

        $data = $this->userGroupMember->delete($group_id, $user_account);

        $this->rong->Group()->quit($user_account,$group_id);
        return $this->response(['result' => $data]);
    }


}